package cn.zn.controller.file;

import cn.zn.base.bean.dto.ResultData;
import cn.zn.base.common.utils.ResultUtil;
import cn.zn.oss.dto.FileDto;
import cn.zn.oss.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 附件
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 通用下载请求
     *
     * @param fileName 文件名称
     */
    @GetMapping("/download")
    public void fileDownload(String fileName, String filePath, HttpServletResponse response) throws Exception {
        fileService.downloadFile(fileName, filePath, response);
    }

    /**
     * 通用上传请求
     */
    @PostMapping("/upload")
    public ResultData uploadFile(MultipartFile file, HttpServletRequest request) throws Exception {
        FileDto fileDto = fileService.uploadFile(file, request);
        return ResultUtil.success(fileDto);
    }

}
